package com.googlecode.openauth.web.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.googlecode.openauth.web.dao.IActionDao;
import com.googlecode.openauth.web.entity.ActionInfo;

public class ActionDaoTest {

	private static ApplicationContext context;
	private static IActionDao action;
	private static SqlSession session;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		DefaultSqlSessionFactory defaultF = (DefaultSqlSessionFactory) context
				.getBean("sqlSessionFactory");
		session = defaultF.openSession(true);
		action = (IActionDao) context.getBean("actionDao");
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Test
	public void testGetAll() {
		// List<ActionInfo> list = action.getAction();
		// if (list != null) {
		// for (ActionInfo au : list) {
		// System.out.println(au.getActionId() + " " + au.getUuid() +
		// au.getActionName() + " " + au.getActionDesc());
		// }
		// }
	}

	// @Test
	public void testAddAuth() {
		ActionInfo temp = new ActionInfo();
		temp.setActionName("action1");
		temp.setActionDesc("action1desc");
		action.addAction(temp);
		System.out.println();
	}

	// @Test
	public void testUpdateAuth() {
		ActionInfo temp = new ActionInfo();
		temp.setActionId(1);
		temp.setActionName("action1modify");
		temp.setActionDesc("action1desc");
		action.updateAction(temp);
	}

	// @Test
	public void testDelAuth() {
		ActionInfo temp = new ActionInfo();
		temp.setActionId(2);
		action.delAction(temp);
	}

	// @Test
	public void testGetAuth() {
		// List<ActionInfo> list = action.getActionByName("action");
		// if (list != null) {
		// for (ActionInfo au : list) {
		// System.out.println(au.getActionId() + " " + au.getUuid() +
		// au.getActionName() + " " + au.getActionDesc());
		// }
		// }
		// List<ActionInfo> list1 = action.getActionByDesc("desc");
		// if (list1 != null) {
		// for (ActionInfo au : list1) {
		// System.out.println(au.getActionId() + " " + au.getUuid() +
		// au.getActionName() + " " + au.getActionDesc());
		// }
		// }
	}

	// @Test
	public void testSearchAction1() {
		ActionInfo temp = new ActionInfo();
		temp.setActionId(2);
		RowBounds rowBounds = new RowBounds(1, 2);
		List<ActionInfo> actions = action.searchActions(temp, rowBounds);
		System.out.println("search 1 : " + actions);
	}

	// @Test
	public void testSearchAction2() {
		ActionInfo temp = new ActionInfo();
		temp.setUuid("fsafsdf");
		RowBounds rowBounds = new RowBounds(1, 2);
		List<ActionInfo> actions = action.searchActions(temp, rowBounds);
		System.out.println("search 2 : " + actions);
	}

	// @Test
	public void testSearchAction3() {
		ActionInfo temp = new ActionInfo();
		temp.setActionName("ac");
		RowBounds rowBounds = new RowBounds(1, 2);
		List<ActionInfo> actions = action.searchActions(temp, rowBounds);
		System.out.println("search 3 : " + actions);
	}

	// @Test
	public void testSearchAction4() {
		ActionInfo temp = new ActionInfo();
		temp.setActionDesc("ac");
		RowBounds rowBounds = new RowBounds(1, 2);
		List<ActionInfo> actions = action.searchActions(temp, rowBounds);
		System.out.println("search 4 : " + actions);
	}

	// @Test
	public void testSearchAction5() {
		ActionInfo temp = new ActionInfo();
		RowBounds rowBounds = new RowBounds(1, 2);
		List<ActionInfo> actions = action.searchActions(temp, rowBounds);
		System.out.println("search 5 : " + actions);
	}

	@Test
	public void TestPage() {
		System.out.println("search 5 : ");
		ActionInfo temp = new ActionInfo();
		temp.setActionName("ac");
		RowBounds rowBounds = new RowBounds(1, 2);
		List<ActionInfo> actions = action.searchActions(temp, rowBounds);
		System.out.println("search 3 : " + actions);
	}

}
